home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / Weubblers handler.adf / examples / SafeFormat.c < prev    next >
C/C++ Source or Header  |  1989-01-15  |  5KB  |  185 lines

  1. int Drive;
  2. int numsecs;
  3. int numtracks;
  4. int numheads;
  5. unsigned char *SectorBuffer;
  6. unsigned short *TrackBuffer,*CheckBuffer,*WriteBuffer;
  7.  
  8. main(argc,argv)
  9. int argc;
  10. char **argv;
  11. {
  12. int List[30];
  13. int drive,track,head;
  14. char c;
  15. int Error,i;
  16.  
  17. drive=0;
  18. numsecs=10;
  19. numtracks=80;
  20. numheads=2;
  21.  
  22. while (argc-->1) 
  23. {i=atoi(argv[argc]);
  24.  if (i<4) drive=i;
  25.  else if (i<12) numsecs=i;
  26.  else numtracks=i;}
  27.  
  28. printf("WARNING!!! This program will format the disk in drive %d, Format:\n        %d sectors/Track,%d tracks/disk, %d Heads - MSDOS\n",drive,numsecs,numtracks,numheads);
  29. printf("IS THIS WHAT YOU REALLY WANT TO DO????\n");
  30. scanf("%c",&c);
  31. if (toupper(c)!='Y') {printf("Poooh... Finishing.\n");_abort();}
  32.  
  33. /* Do the Initialization stuff */
  34. Drive=drive;
  35. InitMotor();
  36. TrackBuffer=(unsigned short *)AllocChipMem(2*TrackLen);
  37. CheckBuffer=(unsigned short *)AllocChipMem(2*TrackLen);
  38. WriteBuffer=(unsigned short *)AllocChipMem(2*TrackLen);
  39. SectorBuffer=(unsigned char *)AllocNormalMem(512);
  40.  
  41. /* First, Create an Empty Track */
  42. CreateEmptyStandardTrack(TrackBuffer,0,0,numsecs);
  43. CreateList(TrackBuffer,List);
  44. for (track=0;track<numtracks;track++)
  45.  for (head=0;head<numheads;head++)
  46.   {
  47.   /* Now adjust the TrackMarks */
  48.   ChangeStandardTrack(TrackBuffer,track,head,numsecs);
  49.  
  50.   again:
  51.   Chk_Abort();
  52.   for (i=0;i<4;i++)
  53.   {
  54.  
  55.   /* Now Write the Track to Disk */
  56.   Error=WriteTrack(TrackBuffer,drive,track,head);
  57.  
  58.   /* And now check the result (obsolete with good disks) */
  59.   if (Error==0) Error=CheckTrackList(TrackBuffer,drive,track,head,List,CheckBuffer);
  60.  
  61.   if (Error==0) break;
  62.   printf("Retrying Track %d...\n",track);
  63.   }
  64.   /* Errors have occured-Request abort */
  65.   if (Error) {aprintf(stderr,"Error writing track %d.\n",track);goto again;}
  66.   }
  67. /* Done with this disk - Now write the Format-Sectors and Bytes */
  68.   WriteMsFormat();
  69. _abort();
  70. }
  71.  
  72. WriteMsFormat()
  73. {
  74.  int List[30];
  75.  int WrList[30];
  76.  int FormatByte;
  77.  int lenfat;
  78.  int drive=Drive;
  79.  int blocknum;
  80.  
  81.  onceagain:
  82.  CreateStandardBootSector(SectorBuffer,numtracks,numheads,numsecs);
  83.  
  84.  /* Calculate Formats for non-3.2 Disks */
  85.  FormatByte=0xff;
  86.  if ((numtracks==80)&&(numsecs==9)&&(numheads==2))
  87.   {
  88.    FormatByte=0xf9;
  89.    SectorBuffer[21]=FormatByte;
  90.  
  91.    /* The Specifications for 3.3 are not precise
  92.       Use one of the following, just as your DOS prefers:
  93.    SectorBuffer[22]=5; 5 Sektoren fuer FAT
  94.    SectorBuffer[13]=1; 1 Sektor pro Cluster
  95.    SectorBuffer[17]=0xe0; Oder vielleicht 0xe0 Directoryeintraege?
  96.      PCDOS 3.3 versteht diesen: */
  97.    SectorBuffer[22]=2; /* 2 Sektoren fuer FAT */
  98.   }
  99.  if ((numtracks==40)&&(numsecs==9)&&(numheads==2))
  100.   {
  101.    FormatByte=0xfd;
  102.    SectorBuffer[21]=FormatByte;
  103.    SectorBuffer[22]=2; /* 2 Sektoren fuer die FAT */
  104.   }
  105.  if (FormatByte==0xff)
  106.   {
  107.   /* No Standard-MSDOS-Format! */;
  108.   FormatByte=SectorBuffer[21];
  109.   }
  110.  
  111.  /* Now, read in the very first track and prepare it for writing back */
  112.  if (ReadTrackList(TrackBuffer,drive,0,0,List)) goto failure;
  113.  CreateStandardTrack(WriteBuffer,numsecs);
  114.  if(CopyList(WriteBuffer,TrackBuffer,numsecs,WrList,List)) goto failure;
  115.  
  116.  /* Now, write the BootBlock into the first Sector */
  117.  if (WriteList(WriteBuffer,SectorBuffer,1,WrList)) goto failure;
  118.  
  119.  /*Now, write the FormatBytes to the FATs, assuming they both reside in Track 1*/
  120.  
  121.  lenfat=SectorBuffer[22];
  122.  blocknum=SectorBuffer[14];
  123.  if (ReadList(WriteBuffer,SectorBuffer,blocknum+1,WrList)) goto failure;
  124.  SectorBuffer[0]=FormatByte;
  125.  SectorBuffer[1]=0xff;
  126.  SectorBuffer[2]=0xff;
  127.  if (WriteList(WriteBuffer,SectorBuffer,blocknum+1,WrList)) goto failure;
  128.  
  129.  blocknum+=lenfat;
  130.  if (ReadList(WriteBuffer,SectorBuffer,blocknum+1,WrList)) goto failure;
  131.  SectorBuffer[0]=FormatByte;
  132.  SectorBuffer[1]=0xff;
  133.  SectorBuffer[2]=0xff;
  134.  if (WriteList(WriteBuffer,SectorBuffer,blocknum+1,WrList)) goto failure;
  135.  
  136.  /*TATA-We're done, Write the Track to Disk and say OK! */
  137.  if (WriteTrack(WriteBuffer,drive,0,0)) goto failure;
  138.  
  139. return(0);
  140. failure:
  141.  aprintf(stderr,"Error writing Diskinfos!!");
  142.  goto onceagain;
  143. }
  144.  
  145.  
  146. CheckTrackList(TrackBuffer,drive,track,head,List,CheckBuffer)
  147. int drive,track,head;
  148. unsigned short *TrackBuffer,*CheckBuffer;
  149. int *List;
  150. {
  151.  int Error,i,k;
  152.  int CheckList[30];
  153.  long *old,*new;
  154.  
  155.  /* Read in the whole Track first */
  156.  if (ReadTrackList(CheckBuffer,drive,track,head,CheckList)) return(-1);
  157.  /* For all Sectors that existed in the old Track: */
  158.  for (i=2;List[i]!=0;i+=2)
  159.      {
  160.      /* Are the old Sectors present in the new Track?*/
  161.      if ((CheckList[i]==0)||(CheckList[i+1]==0)) return(-1);
  162.      /* Yes, So check the AddressSpace first: */
  163.      old=(long *) (TrackBuffer+List[i]);
  164.      new=(long *) (CheckBuffer+CheckList[i]);
  165.      for (k=0;k<3;k++)
  166.          if (*old++!=*new++)
  167.          {
  168.           return(-1);
  169.          }
  170.       if (*(short *)old!=*(short *)new)
  171.       {
  172.        return(-1);
  173.       }
  174.      /* And now, check the DataSpace: */
  175.      old=(long *) (TrackBuffer+List[i+1]);
  176.      new=(long *) (CheckBuffer+CheckList[i+1]);
  177.      for (k=0;k<258;k++)
  178.          if (*old++!=*new++)
  179.          {
  180.           return(-1);
  181.          }
  182.     }
  183.  return(0);
  184. }
  185.